package com.irdstudio.efp.esb.service.mock.zx;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.irdstudio.cdp.pboc.service.vo.ZXHtmlInfoVO;
import com.irdstudio.efp.esb.common.constant.ESBTradeNoConstance;
import com.irdstudio.efp.esb.common.constant.ZXReturnType;
import com.irdstudio.efp.esb.service.bo.req.BaseZXReqBean;
import com.irdstudio.efp.esb.service.bo.req.ZX10001QueryReports;
import com.irdstudio.efp.esb.service.bo.req.ZX10001QueryReports.Builder;
import com.irdstudio.efp.esb.service.bo.resp.zx.BaseZXResBean;
import com.irdstudio.efp.esb.service.bo.resp.zx.ZX10001HTMLResponseReport;
import com.irdstudio.efp.esb.service.vo.zx.ZXVO;
import com.irdstudio.basic.framework.core.exception.ESBException;

/**
 * @description:征信单笔查询接口-HTML
 * @date:2018年12月20日 下午2:30:57
 * @author:hengyh
 */
@Service("zx10001HTMLService")
@DependsOn("ZXConfig")// 确保ZXConfig配置类bean先被容器创建
public class ZX10001HTMLServiceImpl extends BaseZXService<ZX10001QueryReports, ZX10001HTMLResponseReport> {

	private static Logger log = LoggerFactory.getLogger(ZX10001HTMLServiceImpl.class);
	public static final String INTERFACE_ID = "10001";
	private final String vertType = "30";

	@Value("${zx.validDay}")
	private String dayTime;

	@Override
	protected void preProcess(BaseZXReqBean<ZX10001QueryReports> reqBean) {
		ZX10001QueryReports queryReports = reqBean.getQryRptsInfArry().get(0);
		queryReports = new Builder()
				.buildWithExists(queryReports)
				.withIntfInd(INTERFACE_ID)
				.withRptFmt(vertType)
				.withRptAgng(dayTime)
				.withCertTp("0")
				.withRtnType(ZXReturnType.HTML.VALUE)
				.build();
		log.debug("进入征信单笔查询接口，参数：" + Objects.toString(queryReports));
	}

	@Override
	protected ZXVO postProcess(BaseZXReqBean<ZX10001QueryReports> reqBean,
							   BaseZXResBean<ZX10001HTMLResponseReport> resBean) throws ESBException {
		try {
//			String content = Optional.ofNullable(resBean.getContent())
//					.orElseThrow(() -> new ESBException("HTML格式征信查询返回内容为空！"));
			String content = "mock html";
			String cusName = reqBean.getQryRptsInfArry().get(0).getCustNm();
			String certCode = reqBean.getQryRptsInfArry().get(0).getCertNo();
			ZXHtmlInfoVO zxHtmlInfo = new ZXHtmlInfoVO(cusName, certCode, content);
			return new ZXVO(Boolean.TRUE, zxHtmlInfo);
		} catch (Exception e) {
			return new ZXVO(Boolean.FALSE, null);
		}
	}

	@Override
	protected String tradeNo() {
		return ESBTradeNoConstance.ZX10001;
	}

	@Override
	protected String scene() {
		return "01";
	}

	@Override
	protected Function<String, BaseZXResBean<ZX10001HTMLResponseReport>> converter() {
		return i -> JSONObject.parseObject(i, new TypeReference<BaseZXResBean<ZX10001HTMLResponseReport>>() {});
	}
}
